package com.micro.frog.acct.rest;

import com.micro.frog.acct.dto.UserDTO;
import com.micro.frog.acct.dto.UserDetail;
import com.micro.frog.acct.entity.User;
import com.micro.frog.acct.service.UserService;
import com.micro.frog.acct.utils.SubjectUtil;
import com.micro.frog.acct.vo.UserVO;
import com.micro.frog.core.common.PageResult;
import com.micro.frog.core.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author :  ljh
 * @date : 2018/4/11
 **/
@Slf4j
@RestController
@RequestMapping("/rbac/user")
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping(value = "/all")
    public Result all(){
        Result result = new Result();
        try{
            result.setFlag(true);
            result.setData(userService.all());
        }catch (Exception e){
            result.setCode("20000");
            result.setMsg("系统异常" + e.getMessage());
            log.error("系统异常，e={}",e);
        }
        return result;
    }

    @GetMapping(value = "/list")
    public PageResult list(UserDTO queryVO){
        PageResult result = new PageResult();
        try{
            result = userService.list(queryVO);
        }catch (Exception e){
            result.setCode("20000");
            result.setMsg("系统异常");
            log.error("系统异常，e={}", e);
        }
        return result;
    }

    @PostMapping("/add")
    public Result add(UserVO userVO) {
        Result result = new Result();
        try{
            result = userService.add(userVO);
        }catch (Exception e){
            result.setFlag(false);
            result.setMsg("新增失败" + e.getMessage());
            log.error("新增失败 e:{}", e);
        }
        return result;
    }

    @PostMapping("/delete")
    public Result delete(Integer id) {
        Result result = new Result();
        try{
            userService.delete(id);
            result.setFlag(true);
            result.setMsg("删除成功");
        }catch (Exception e){
            result.setFlag(false);
            result.setMsg("删除失败");
            log.error("系统异常，e={}", e);
        }
        return result;
    }

    @PostMapping("/update")
    public Result update(UserVO userVO) {
        Result result = new Result();
        try{
            userService.update(userVO);
            result.setFlag(true);
            result.setMsg("修改成功");
        }catch (Exception e){
            result.setFlag(false);
            result.setMsg("修改失败,系统异常" + e.getMessage());
            log.error("系统异常，e={}", e);
        }
        return result;
    }

    @PostMapping("/updateStatus")
    public Result update(Integer id, Integer status, HttpServletRequest request) {
        Result result = new Result();
        try{
            final Date current = new Date();
            UserDetail userDetail = SubjectUtil.getUser(request);
            User user = new User();
            user.setId(id);
            user.setStatus(status);
            user.setUpdatedBy(userDetail.getUserName());
            user.setUpdatedTime(current);
            result = userService.updateStatus(user);
        }catch (Exception e){
            result.setFlag(false);
            result.setMsg("修改失败" + e.getMessage());
            log.error("系统异常，e={}", e);
        }
        return result;
    }
}
